package com.sisreq.elementos;

import javax.swing.JPasswordField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;

@SuppressWarnings("serial")
public class MyPasswordField extends JPasswordField{
	
	public MyPasswordField (int tamanho){
		PlainDocument doc = (PlainDocument) this.getDocument();
		doc.setDocumentFilter(new TextLengthDocFilter(tamanho));
		this.setDocument(doc);
	}
	
	private class TextLengthDocFilter extends DocumentFilter {
		private int maxTextLength;

		public TextLengthDocFilter(int maxTextLength) {
			this.maxTextLength = maxTextLength;
		}

		private boolean verifyText(String text) {
			return text.length() <= maxTextLength;
		}

		@Override
		public void insertString(FilterBypass fb, int offset, String string,
				AttributeSet attr) throws BadLocationException {

			Document doc = fb.getDocument();
			String oldText = doc.getText(0, doc.getLength());
			StringBuilder sb = new StringBuilder(oldText);
			sb.insert(offset, string);

			if (verifyText(sb.toString())) {
				super.insertString(fb, offset, string, attr);
			}

		}

		@Override
		public void replace(FilterBypass fb, int offset, int length,
				String text, AttributeSet attrs) throws BadLocationException {
			Document doc = fb.getDocument();
			String oldText = doc.getText(0, doc.getLength());
			StringBuilder sb = new StringBuilder(oldText);

			sb.replace(offset, offset + length, text);
			if (verifyText(sb.toString())) {
				super.replace(fb, offset, length, text, attrs);
			}
		}

		@Override
		public void remove(FilterBypass fb, int offset, int length)
				throws BadLocationException {
			Document doc = fb.getDocument();
			String oldText = doc.getText(0, doc.getLength());
			StringBuilder sb = new StringBuilder(oldText);

			sb.replace(offset, offset + length, "");

			if (verifyText(sb.toString())) {
				super.remove(fb, offset, length);
			}
		}
	}
}
